# coding:utf-8
"""
@author: jing
@contact: 529548204@qq.com
@file: pagination.py
@time: 2022/7/11 13:56
"""
from rest_framework.exceptions import NotFound
from rest_framework.pagination import PageNumberPagination


class MyPagePagination(PageNumberPagination):
    page_size = 20
    page_size_query_param = 'page_size'
    page_query_param = 'page'
    max_page_size = 100

    def paginate_queryset(self, queryset, request, view=None):
        # if request.query_params.get('get_all'):
        #     return None
        try:
            qs = super().paginate_queryset(queryset, request, view=view)
            return qs
        except NotFound:
            return None

    def get_paginated_response(self, data):
        try:
            data = {
                'next': self.get_next_link(),
                'previous': self.get_previous_link(),
                'count': self.page.paginator.count,
                'data': data
            }
        except Exception:
            data = {'data': []}
        return data
